package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yanglup
 * @create 2020-12-16 13:46
 */
@Controller
public class BackUploadfileController {
    /***用于上传图片页面使用---上传页面*/
    @PostMapping(value = "/fileUploadshangchuan")
    public String fileUploadshangchuan(@RequestParam(value = "file") MultipartFile file, Model model, HttpServletRequest request, HttpSession session) {
        String myfilePath = "";
        String filename = BackUploadfileutil.upload(file);
        myfilePath = request.getScheme() + "://" +
                request.getServerName() + ":"
                + request.getServerPort()
                + "/img/" + filename;
        System.out.println("最后："+myfilePath);
        model.addAttribute("filename", myfilePath);
        session.setAttribute("filename",myfilePath);
//        return "redirect:/myuploadfile.html";
        return "myuploadfile";
    }

    /**用于上传图片，且返回图片地址---部分提价上传*/
    @RequestMapping(value = "/fileUpload", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> fileUpload(@RequestParam(value = "file") MultipartFile file, Model model, HttpServletRequest request, HttpSession session) {
        Map<String, String> ret = new HashMap<String, String>();
        String myfilePath = "";
        String filename = BackUploadfileutil.upload(file);
        myfilePath = request.getScheme() + "://" +
                request.getServerName() + ":"
                + request.getServerPort()
                + "/img/" + filename;
        System.out.println("最后："+myfilePath);
        ret.put("filename", myfilePath);
        return ret;
    }


}
